﻿using WCS.Entities;
using WCS.Entities.Enum;
using WCS.IServices;

namespace WCS.Services.MonitorService
{


    /// <summary>
    /// 标准设备的通信方式，参考文档
    /// </summary>
    public class CommonMonitorService : IDeviceMonitor
    {
        public void EvaluationState(TransferPort port)
        {

            if (port.PlcValue == 0)
            {
                port.Status = port.AgvValue == 0 ? PortStatus.Idle : PortStatus.Reset;


            }
            else if ((port.PlcValue & 0xff) > 0) //取前8位
            {
                port.Status = (port.AgvValue & 0xff) == 0 ? PortStatus.Calling : PortStatus.Called;
            }
            //else if (BitHelper.GetBitValue(port.PlcValue, 4))//急停通知
            //{
            //    port.Status = BitHelper.GetBitValue(port.AgvValue, 4) ? PortStatus.Estoped : PortStatus.Estoping;
            //}
            if (BitHelper.GetBitValue(port.PlcValue, 10))//任务取消通知
            {
                port.PLcDisplayInfo["Cancel"] = 1;
                if (BitHelper.GetBitValue(port.AgvValue, 10))
                {
                    port.Status = PortStatus.Canceled;
                    port.AgvDisplayInfo["ResCancel"] = 1;
                }
                else
                {
                    port.Status = BitHelper.GetBitValue(port.AgvValue, 11) ? PortStatus.CancelFailed : PortStatus.Canceling;
                    if (port.Status == PortStatus.Canceled)
                        port.AgvDisplayInfo["ResCancel"] = 2;
                }
            }
            else
            {
                port.Status = PortStatus.Excuting;
            }

            var datas = new string(Convert.ToString(port.PlcValue & 0xff, 2).PadLeft(8, '0').Reverse().ToArray());
            var index = datas.IndexOf('1') + 1;
            port.CallType = port.CallType = (TaskType)index;//-1 +1 是0 None，0 位送满料，1位送
            if ((port.AgvValue & 0xff) != 0)
                port.AgvDisplayInfo["Reponse"] = 1;
            else
                port.AgvDisplayInfo["Reponse"] = 0;
            if ((port.PlcValue & 0xff) != 0)
                port.PLcDisplayInfo["Call"] = 1;
            else
                port.PLcDisplayInfo["Call"] = 0;
            port.PLcDisplayInfo["Arrived"] = BitHelper.GetBitValue(port.PlcValue, 8) ? 1 : 0;

            port.PLcDisplayInfo["Finished"] = BitHelper.GetBitValue(port.PlcValue, 9) ? 1 : 0;

            port.AgvDisplayInfo["Arrived"] = BitHelper.GetBitValue(port.AgvValue, 8) ? 1 : 0;

            port.AgvDisplayInfo["Finished"] = BitHelper.GetBitValue(port.AgvValue, 9) ? 1 : 0;

        }

        public void MonitorDevice(Device device)
        {

            foreach (var item in device.TransferPorts.Where(x => x.Enable))
            {
                if (item.PLcDisplayInfo.Count == 0)
                {
                    item.PLcDisplayInfo = new Dictionary<string, int>()
                    {
                        ["Call"] = 0,
                        ["Arrived"] = 0,
                        ["Finished"] = 0,
                        ["Cancel"] = 0,
                    };
                }

                if (item.AgvDisplayInfo.Count == 0)
                {
                    item.AgvDisplayInfo = new Dictionary<string, int>()
                    {
                        ["Reponse"] = 0,
                        ["Arrived"] = 0,
                        ["Finished"] = 0,
                        ["ResCancel"] = 0,
                        ["Cancel"] = 0
                    };
                }

                var result1 = device?.Operation?.ReadInt16(item.AgvAddress);
                if (result1?.IsSucceed ?? false)
                {
                    item.AgvValue = result1.Value;

                }
                var result2 = device?.Operation?.ReadInt16(item.PlcAddress);
                if (result2?.IsSucceed ?? false)
                {
                    item.PlcValue = result2.Value;

                }
                EvaluationState(item);
                if (item.Status == PortStatus.Reset)
                    device?.Operation?.Write(item.AgvAddress, (short)0);
            }

        }

    }
}
